package spark.utility;

import org.apache.log4j.Logger;

public class FrameBreak {
	private static Logger logger = Logger.getLogger(FrameBreak.class);

	private final int FRAME_FREQUENCY;
	private int msSinceLastFrame;

	public FrameBreak(int fps) {
		FRAME_FREQUENCY = 1000 / fps;
		msSinceLastFrame = FRAME_FREQUENCY;
	}

	public void breakFrame(float elapsedMs) {
		msSinceLastFrame += elapsedMs;
		if(msSinceLastFrame < FRAME_FREQUENCY) {
			try {
				Thread.sleep(FRAME_FREQUENCY - msSinceLastFrame);
			}
			catch(InterruptedException ignored) {
				logger.error(ignored);
			}
		}
		else {
			msSinceLastFrame = 0;
		}
	}
}
